package androidx.appcompat.widget;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.content.Context;
import android.content.res.Configuration;
import android.graphics.drawable.Drawable;
import android.text.TextUtils;
import android.text.TextUtils.TruncateAt;
import android.view.View;
import android.view.View.MeasureSpec;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.view.ViewPropertyAnimator;
import android.view.accessibility.AccessibilityEvent;
import android.view.accessibility.AccessibilityNodeInfo;
import android.view.animation.DecelerateInterpolator;
import android.view.animation.Interpolator;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.BaseAdapter;
import android.widget.HorizontalScrollView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Spinner;
import android.widget.TextView;
import androidx.annotation.RestrictTo;
import androidx.appcompat.app.ActionBar.e;
import o.au;
import o.cw;
import o.cz;

@RestrictTo({androidx.annotation.RestrictTo.d.LIBRARY_GROUP})
public class ScrollingTabContainerView extends HorizontalScrollView implements OnItemSelectedListener {
    private static final Interpolator ˊॱ = new DecelerateInterpolator();
    private boolean ʻ;
    private c ʼ;
    private int ʽ;
    int ˊ;
    LinearLayoutCompat ˋ;
    private int ˋॱ;
    protected ViewPropertyAnimator ˎ;
    Runnable ˏ;
    int ॱ;
    private Spinner ॱॱ;
    protected final d ᐝ = new d(this);

    class TabAdapter extends BaseAdapter {
        TabAdapter() {
        }

        public int getCount() {
            return ScrollingTabContainerView.this.ˋ.getChildCount();
        }

        public Object getItem(int i) {
            return ((a) ScrollingTabContainerView.this.ˋ.getChildAt(i)).ˊ();
        }

        public long getItemId(int i) {
            return (long) i;
        }

        public View getView(int i, View view, ViewGroup viewGroup) {
            if (view == null) {
                return ScrollingTabContainerView.this.ˏ((e) getItem(i), true);
            }
            ((a) view).ॱ((e) getItem(i));
            return view;
        }
    }

    class a extends LinearLayout {
        private View ʼ;
        final /* synthetic */ ScrollingTabContainerView ˊ;
        private ImageView ˋ;
        private e ˎ;
        private final int[] ˏ = new int[]{16842964};
        private TextView ॱ;

        public a(ScrollingTabContainerView scrollingTabContainerView, Context context, e eVar, boolean z) {
            this.ˊ = scrollingTabContainerView;
            super(context, null, o.ah.c.ˎ);
            this.ˎ = eVar;
            cw ॱ = cw.ॱ(context, null, this.ˏ, o.ah.c.ˎ, 0);
            if (ॱ.ᐝ(0)) {
                setBackgroundDrawable(ॱ.ˋ(0));
            }
            ॱ.ॱ();
            if (z) {
                setGravity(8388627);
            }
            ˋ();
        }

        public void ॱ(e eVar) {
            this.ˎ = eVar;
            ˋ();
        }

        public void setSelected(boolean z) {
            Object obj = isSelected() != z ? 1 : null;
            super.setSelected(z);
            if (obj != null && z) {
                sendAccessibilityEvent(4);
            }
        }

        public void onInitializeAccessibilityEvent(AccessibilityEvent accessibilityEvent) {
            super.onInitializeAccessibilityEvent(accessibilityEvent);
            accessibilityEvent.setClassName(e.class.getName());
        }

        public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo accessibilityNodeInfo) {
            super.onInitializeAccessibilityNodeInfo(accessibilityNodeInfo);
            accessibilityNodeInfo.setClassName(e.class.getName());
        }

        public void onMeasure(int i, int i2) {
            super.onMeasure(i, i2);
            if (this.ˊ.ˊ > 0 && getMeasuredWidth() > this.ˊ.ˊ) {
                super.onMeasure(MeasureSpec.makeMeasureSpec(this.ˊ.ˊ, 1073741824), i2);
            }
        }

        public void ˋ() {
            e eVar = this.ˎ;
            View ˊ = eVar.ˊ();
            if (ˊ != null) {
                a parent = ˊ.getParent();
                if (parent != this) {
                    if (parent != null) {
                        parent.removeView(ˊ);
                    }
                    addView(ˊ);
                }
                this.ʼ = ˊ;
                if (this.ॱ != null) {
                    this.ॱ.setVisibility(8);
                }
                if (this.ˋ != null) {
                    this.ˋ.setVisibility(8);
                    this.ˋ.setImageDrawable(null);
                    return;
                }
                return;
            }
            int i;
            CharSequence charSequence;
            if (this.ʼ != null) {
                removeView(this.ʼ);
                this.ʼ = null;
            }
            Drawable ॱ = eVar.ॱ();
            CharSequence ˋ = eVar.ˋ();
            if (ॱ != null) {
                if (this.ˋ == null) {
                    View appCompatImageView = new AppCompatImageView(getContext());
                    LayoutParams layoutParams = new LinearLayout.LayoutParams(-2, -2);
                    layoutParams.gravity = 16;
                    appCompatImageView.setLayoutParams(layoutParams);
                    addView(appCompatImageView, 0);
                    this.ˋ = appCompatImageView;
                }
                this.ˋ.setImageDrawable(ॱ);
                this.ˋ.setVisibility(0);
            } else if (this.ˋ != null) {
                this.ˋ.setVisibility(8);
                this.ˋ.setImageDrawable(null);
            }
            if (TextUtils.isEmpty(ˋ)) {
                i = 0;
            } else {
                i = 1;
            }
            if (i != 0) {
                if (this.ॱ == null) {
                    appCompatImageView = new AppCompatTextView(getContext(), null, o.ah.c.ʻ);
                    appCompatImageView.setEllipsize(TruncateAt.END);
                    layoutParams = new LinearLayout.LayoutParams(-2, -2);
                    layoutParams.gravity = 16;
                    appCompatImageView.setLayoutParams(layoutParams);
                    addView(appCompatImageView);
                    this.ॱ = appCompatImageView;
                }
                this.ॱ.setText(ˋ);
                this.ॱ.setVisibility(0);
            } else if (this.ॱ != null) {
                this.ॱ.setVisibility(8);
                this.ॱ.setText(null);
            }
            if (this.ˋ != null) {
                this.ˋ.setContentDescription(eVar.ˏ());
            }
            if (i != 0) {
                charSequence = null;
            } else {
                charSequence = eVar.ˏ();
            }
            cz.ˋ(this, charSequence);
        }

        public e ˊ() {
            return this.ˎ;
        }
    }

    class c implements OnClickListener {
        final /* synthetic */ ScrollingTabContainerView ॱ;

        c(ScrollingTabContainerView scrollingTabContainerView) {
            this.ॱ = scrollingTabContainerView;
        }

        public void onClick(View view) {
            ((a) view).ˊ().ˎ();
            int childCount = this.ॱ.ˋ.getChildCount();
            for (int i = 0; i < childCount; i++) {
                boolean z;
                View childAt = this.ॱ.ˋ.getChildAt(i);
                if (childAt == view) {
                    z = true;
                } else {
                    z = false;
                }
                childAt.setSelected(z);
            }
        }
    }

    protected class d extends AnimatorListenerAdapter {
        private int ˊ;
        private boolean ˋ = false;
        final /* synthetic */ ScrollingTabContainerView ॱ;

        protected d(ScrollingTabContainerView scrollingTabContainerView) {
            this.ॱ = scrollingTabContainerView;
        }

        public void onAnimationStart(Animator animator) {
            this.ॱ.setVisibility(0);
            this.ˋ = false;
        }

        public void onAnimationEnd(Animator animator) {
            if (!this.ˋ) {
                this.ॱ.ˎ = null;
                this.ॱ.setVisibility(this.ˊ);
            }
        }

        public void onAnimationCancel(Animator animator) {
            this.ˋ = true;
        }
    }

    public ScrollingTabContainerView(Context context) {
        super(context);
        setHorizontalScrollBarEnabled(false);
        au ˏ = au.ˏ(context);
        setContentHeight(ˏ.ॱ());
        this.ॱ = ˏ.ʼ();
        this.ˋ = ˊ();
        addView(this.ˋ, new LayoutParams(-2, -1));
    }

    public void onMeasure(int i, int i2) {
        int i3 = 1;
        int mode = MeasureSpec.getMode(i);
        boolean z = mode == 1073741824;
        setFillViewport(z);
        int childCount = this.ˋ.getChildCount();
        if (childCount <= 1 || !(mode == 1073741824 || mode == Integer.MIN_VALUE)) {
            this.ˊ = -1;
        } else {
            if (childCount > 2) {
                this.ˊ = (int) (((float) MeasureSpec.getSize(i)) * 0.4f);
            } else {
                this.ˊ = MeasureSpec.getSize(i) / 2;
            }
            this.ˊ = Math.min(this.ˊ, this.ॱ);
        }
        mode = MeasureSpec.makeMeasureSpec(this.ʽ, 1073741824);
        if (z || !this.ʻ) {
            i3 = 0;
        }
        if (i3 != 0) {
            this.ˋ.measure(0, mode);
            if (this.ˋ.getMeasuredWidth() > MeasureSpec.getSize(i)) {
                ॱ();
            } else {
                ˎ();
            }
        } else {
            ˎ();
        }
        i3 = getMeasuredWidth();
        super.onMeasure(i, mode);
        int measuredWidth = getMeasuredWidth();
        if (z && i3 != measuredWidth) {
            setTabSelected(this.ˋॱ);
        }
    }

    private boolean ˏ() {
        return this.ॱॱ != null && this.ॱॱ.getParent() == this;
    }

    public void setAllowCollapse(boolean z) {
        this.ʻ = z;
    }

    private void ॱ() {
        if (!ˏ()) {
            if (this.ॱॱ == null) {
                this.ॱॱ = ˋ();
            }
            removeView(this.ˋ);
            addView(this.ॱॱ, new LayoutParams(-2, -1));
            if (this.ॱॱ.getAdapter() == null) {
                this.ॱॱ.setAdapter(new TabAdapter());
            }
            if (this.ˏ != null) {
                removeCallbacks(this.ˏ);
                this.ˏ = null;
            }
            this.ॱॱ.setSelection(this.ˋॱ);
        }
    }

    private boolean ˎ() {
        if (ˏ()) {
            removeView(this.ॱॱ);
            addView(this.ˋ, new LayoutParams(-2, -1));
            setTabSelected(this.ॱॱ.getSelectedItemPosition());
        }
        return false;
    }

    public void setTabSelected(int i) {
        this.ˋॱ = i;
        int childCount = this.ˋ.getChildCount();
        for (int i2 = 0; i2 < childCount; i2++) {
            boolean z;
            View childAt = this.ˋ.getChildAt(i2);
            if (i2 == i) {
                z = true;
            } else {
                z = false;
            }
            childAt.setSelected(z);
            if (z) {
                ˏ(i);
            }
        }
        if (this.ॱॱ != null && i >= 0) {
            this.ॱॱ.setSelection(i);
        }
    }

    public void setContentHeight(int i) {
        this.ʽ = i;
        requestLayout();
    }

    private LinearLayoutCompat ˊ() {
        LinearLayoutCompat linearLayoutCompat = new LinearLayoutCompat(getContext(), null, o.ah.c.ˊ);
        linearLayoutCompat.setMeasureWithLargestChildEnabled(true);
        linearLayoutCompat.setGravity(17);
        linearLayoutCompat.setLayoutParams(new LinearLayoutCompat.LayoutParams(-2, -1));
        return linearLayoutCompat;
    }

    private Spinner ˋ() {
        Spinner appCompatSpinner = new AppCompatSpinner(getContext(), null, o.ah.c.ʽ);
        appCompatSpinner.setLayoutParams(new LinearLayoutCompat.LayoutParams(-2, -1));
        appCompatSpinner.setOnItemSelectedListener(this);
        return appCompatSpinner;
    }

    protected void onConfigurationChanged(Configuration configuration) {
        super.onConfigurationChanged(configuration);
        au ˏ = au.ˏ(getContext());
        setContentHeight(ˏ.ॱ());
        this.ॱ = ˏ.ʼ();
    }

    public void ˏ(int i) {
        final View childAt = this.ˋ.getChildAt(i);
        if (this.ˏ != null) {
            removeCallbacks(this.ˏ);
        }
        this.ˏ = new Runnable(this) {
            final /* synthetic */ ScrollingTabContainerView ˎ;

            public void run() {
                this.ˎ.smoothScrollTo(childAt.getLeft() - ((this.ˎ.getWidth() - childAt.getWidth()) / 2), 0);
                this.ˎ.ˏ = null;
            }
        };
        post(this.ˏ);
    }

    public void onAttachedToWindow() {
        super.onAttachedToWindow();
        if (this.ˏ != null) {
            post(this.ˏ);
        }
    }

    public void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        if (this.ˏ != null) {
            removeCallbacks(this.ˏ);
        }
    }

    a ˏ(e eVar, boolean z) {
        a aVar = new a(this, getContext(), eVar, z);
        if (z) {
            aVar.setBackgroundDrawable(null);
            aVar.setLayoutParams(new AbsListView.LayoutParams(-1, this.ʽ));
        } else {
            aVar.setFocusable(true);
            if (this.ʼ == null) {
                this.ʼ = new c(this);
            }
            aVar.setOnClickListener(this.ʼ);
        }
        return aVar;
    }

    public void onItemSelected(AdapterView<?> adapterView, View view, int i, long j) {
        ((a) view).ˊ().ˎ();
    }

    public void onNothingSelected(AdapterView<?> adapterView) {
    }
}
